package com.ruoyi.workflow.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.workflow.utils.entry.TemplateDataVo;
import com.ruoyi.workflow.utils.entry.WxMssVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class WeChatUtil {

    @Autowired
    private RestTemplate restTemplate;

    public static JSONObject getSessionKeyOrOpenId(String code) {
        //微信小程序官方接口
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        //接口所需参数
        HashMap<String, Object> requestUrlParam = new HashMap<>();
        //小程序appId
        requestUrlParam.put("appid", "wx7932d17a3aa608dc");
        //小程序secret
        requestUrlParam.put("secret", "722bf6ab5967dd8ff62a6618ab51ec40");
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数，固定写死即可
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        String result = HttpUtil.get(requestUrl, requestUrlParam);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        String openid = jsonObject.get("openid", String.class);
        return jsonObject;
    }

    /**
     * description 1.获取用户的临时code
     * param [appid, redirectUrl]
     * return java.lang.String
     * author
     **/
    public static String getUserUathUrl(String appid, String redirectUrl) throws UnsupportedEncodingException {
        StringBuffer getcodeUrl = new StringBuffer()
            .append("https://open.weixin.qq.com/connect/oauth2/authorize")
            .append("?appid=" + appid)
            .append("&redirect_uri=" + URLEncoder.encode(redirectUrl, "utf-8"))
            .append("&response_type=code")
            .append("&scope=snsapi_userinfo")
            .append("&state=" + System.currentTimeMillis())
            .append("#wechat_redirect");

        return getcodeUrl.toString();
    }

    /**
     * description  2.获取用户的openid和access_token
     * param [appid, appSecret, code]
     * return java.lang.String
     * author
     **/
    public static String getBaseAccessTokenUrl(String appid, String appSecret, String code) throws UnsupportedEncodingException {
        StringBuffer baseAccessTokenUrl = new StringBuffer()
            .append("https://api.weixin.qq.com/sns/oauth2/access_token")
            .append("?appid=" + appid)
            .append("&secret=" + appSecret)
            .append("&code=" + code)
            .append("&grant_type=authorization_code");

        return baseAccessTokenUrl.toString();
    }

    /**
     * description  3.根据openid 获取用户的信息
     * param [accessToken, openid]
     * return java.lang.String
     * author
     **/
    public static String getBaseUserInfoUrl(String accessToken, String openid) {
        StringBuffer baseUserInfoUrl = new StringBuffer()
            .append("https://api.weixin.qq.com/sns/userinfo")
            .append("?access_token=" + accessToken)
            .append("&openid=" + openid)
            .append("&lang=zh_CN");
        return baseUserInfoUrl.toString();
    }

    /**
     * description 4检验授权凭证（access_token）是否有效
     * param [openid, accessToken]
     * return java.lang.String
     **/
    public static String checkAccessToken(String openid, String accessToken) {
        StringBuffer stringBuffer = new StringBuffer().append(" https://api.weixin.qq.com/sns/auth")
            .append("?access_token=" + accessToken)
            .append("&openid=" + openid);
        return stringBuffer.toString();
    }

    /**
     * description 微信小程序登录，通过code获取session_key和openid
     * param [appid, secret, code]
     * return java.lang.String
     * author
     **/
    public static String getCode2Session(String appid, String secret, String code) {
        StringBuffer code2Session = new StringBuffer()
            .append("ttps://api.weixin.qq.com/sns/jscode2session")
            .append("?appid=" + appid)
            .append("&secret=" + secret)
            .append("&js_code=" + code)
            .append("&grant_type=authorization_code");
        return code2Session.toString();
    }

    /*
     * 获取access_token
     * appid和appsecret到小程序后台获取，当然也可以让小程序开发人员给你传过来
     * */
    public String getAccess_token() {
        //获取access_token
        String appid = "wx7932d17a3aa608dc";
        String appsecret = "722bf6ab5967dd8ff62a6618ab51ec40";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
            "&appid=" + appid + "&secret=" + appsecret;
        if(restTemplate==null){
            restTemplate = new RestTemplate();
        }
        String json = restTemplate.getForObject(url, String.class);
        JSONObject myJson = JSONUtil.parseObj(json);
        return myJson.get("access_token").toString();
    }

    /**
     *    推送消息给指定的用户
     * @param access_token  app的token
     * @param openid 用户openid
     * @param type 类型 1派发模板 2.反馈提醒 3审核模板 4日期截至提醒模板
     * @param templateId 模板ID
     * @param keywords {与模板字段一一对应}
     * @return
     */
    public String pushOneUser(String access_token, String openid, String type, String templateId, String[] keywords) {

        //如果access_token为空则从新获取
        if(StringUtils.isEmpty(access_token)){
            access_token = getAccess_token();
        }

        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" +
            "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        /*wxMssVo.setForm_id(formId);//formId*/
        wxMssVo.setTemplate_id(templateId);//模版id
        Map<String, TemplateDataVo> m = new HashMap<>();
        if (type.equals("3")) {
            m.put("thing13", new TemplateDataVo(keywords[0]));
            m.put("phrase14", new TemplateDataVo(keywords[1]));
            m.put("phrase1", new TemplateDataVo(keywords[2]));
            m.put("date2", new TemplateDataVo(keywords[3]));
            m.put("date5", new TemplateDataVo(keywords[4]));
            wxMssVo.setData(m);
        } else if (type.equals("1")) {
            /* [发起人,发布时间,任务名称,任务描述,结束时间]*/
            m.put("name7",new TemplateDataVo(keywords[0]));
            m.put("thing5",new TemplateDataVo(keywords[1]));
            m.put("thing2",new TemplateDataVo(keywords[2]));
            m.put("date3",new TemplateDataVo(keywords[3]));
            wxMssVo.setData(m);
        }else if (type.equals("2")){

        }

        if(restTemplate==null){
            restTemplate = new RestTemplate();
        }

        ResponseEntity<String> responseEntity =
            restTemplate.postForEntity(url, wxMssVo, String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

    public static void main(String[] args) {

        WeChatUtil wxService = new WeChatUtil();
        System.out.println(wxService.getAccess_token());
        String values[] = {"zzh7878","2023年5月8日 15:01","ceshi"};
        String values2[] = {"张三","未打卡","上班打卡","08:30","2023-08-17"};
        wxService.pushOneUser(wxService.getAccess_token()
            , "oqDyV5L_RAV4XfSyoKh5KeNzmmew", "3",
            "nmDVqrlgLKmwRJMiKeLrriOgMFNAPTxEgqf-DkBEfJg"
            , values2);
    }
}
